﻿using ProductSystem.BLL;
using ProductSystem.BLL.Common;
using ProductSystem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ProductSystem.Web.Controllers
{
    public class ConfigurationController : ManageControllerBase
    {
        #region 命令配置
        
        /// <summary>
        /// 命令配置添加页面
        /// </summary>
        /// <returns></returns>
        [ManageAuthorize(RequireCheckRole = true, ActionName = "Configuration-ImageCommandConfigList")]
        public ActionResult ImageCommandConfigAdd()
        {
            return View();
        }
        
        #endregion
        #region 服务配置
        /// <summary>
        /// 时间配置
        /// </summary>
        /// <param name="dataType">数据类型</param>
        /// <returns></returns>
        [ManageAuthorize(RequireCheckRole = true, ActionName = "Configuration-ServerAppConfigList")]
        public ActionResult ServerAppConfigList(string keyword)
        {
            PageInfo pageInfo = GetPageInfo();
            List<ServerAppConfig> list = ServerAppConfigBll.GetList(keyword, pageInfo);
            ViewData.Model = list;
            ViewData["keyword"] = keyword;
            ViewBag.PageSize = pageInfo.PageSize;
            ViewBag.CurrentPage = pageInfo.CurPage;
            ViewBag.TotalCount = pageInfo.TotalCount;
            return View();
        }

        /// <summary>
        /// 命令配置添加页面
        /// </summary>
        /// <returns></returns>
        [ManageAuthorize(RequireCheckRole = true, ActionName = "Configuration-ServerAppConfigList")]
        public ActionResult ServerAppConfigAdd()
        {
            return View();
        }

        /// <summary>
        /// 命令配置添加
        /// </summary>
        /// <param name="model">数据元</param>
        /// <returns></returns>
        [HttpPost]
        [ManageAuthorize(RequireCheckRole = true, ActionName = "Configuration-ServerAppConfigList")]
        public ActionResult ServerAppConfigAdd(ServerAppConfig model)
        {
            model.CreateTime = DateTime.Now;
            LoginUser currentUser = AppUserSession.Instance().GetCurrentUser();
            model.CreateAccount = currentUser.Account;
            DataAddGeneric<ServerAppConfig>.isExist = ServerAppConfigBll.IsExist;
            ReturnStatus rs = DataAddGeneric<ServerAppConfig>.Add(model);
            return Json(rs, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 命令配置编辑页面
        /// </summary>
        /// <param name="id">Id</param>
        /// <returns></returns>
        [ManageAuthorize(RequireCheckRole = true, ActionName = "Configuration-ServerAppConfigList")]
        public ActionResult ServerAppConfigEdit(int id)
        {
            ServerAppConfig data = ServerAppConfigBll.GetData(id);
            ViewData.Model = data;
            return View();
        }

        /// <summary>
        /// 命令配置编辑
        /// </summary>
        /// <param name="id">Id</param>
        /// <param name="collection">数据元</param>
        /// <returns></returns>
        [HttpPost]
        [ManageAuthorize(RequireCheckRole = true, ActionName = "Configuration-ServerAppConfigList")]
        public ActionResult ServerAppConfigEdit(int id, FormCollection collection)
        {
            ReturnStatus rs = new ReturnStatus();
            try
            {
                using (EntitiesModel dbContext = new EntitiesModel())
                {
                    ServerAppConfig model = dbContext.ServerAppConfigs.FirstOrDefault(o => o.Id == id);
                    if (TryUpdateModel(model))
                    {
                        if (!ServerAppConfigBll.IsExist(model))
                        {
                            model.UpdateTime = DateTime.Now;
                            LoginUser currentUser = AppUserSession.Instance().GetCurrentUser();
                            model.UpdateAccount = currentUser.Account;
                            dbContext.SaveChanges();
                            rs = PromptMessage.Instance().GetPrompt(true, TBConfig.Instance().GetPromptEditSuccess);
                        }
                        else
                        {
                            rs = PromptMessage.Instance().GetPrompt(false, TBConfig.Instance().GetPromptEditExist);
                        }
                    }
                    else
                    {
                        rs = PromptMessage.Instance().GetPrompt(false, TBConfig.Instance().GetPromptEditFail);
                    }
                }
            }
            catch
            {
                rs = PromptMessage.Instance().GetPrompt(false, TBConfig.Instance().GetPromptEditFail);
            }
            return Json(rs, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 命令配置删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [ManageAuthorize(RequireCheckRole = true, ActionName = "Configuration-ServerAppConfigList")]
        public ActionResult DelServerAppConfig(string ids)
        {
            string[] idArray = ids.Split(',');
            try
            {
                using (EntitiesModel dbContext = new EntitiesModel())
                {
                    var list = dbContext.ServerAppConfigs.Where(t => idArray.Contains(t.Id.ToString()));
                    dbContext.Delete(list);
                    dbContext.SaveChanges();
                }
                return Json(PromptMessage.Instance().GetPrompt(true, TBConfig.Instance().GetPromptDeleteSuccess), JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(PromptMessage.Instance().GetPrompt(false, TBConfig.Instance().GetPromptDeleteFail), JsonRequestBehavior.AllowGet);
            }
        }
        #endregion
    }
}
